package com.kylinhunter.hibernate.dao;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Chcode entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.kylinhunter.hibernate.dao.Chcode
 * @author MyEclipse Persistence Tools
 */

public class ChcodeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ChcodeDAO.class);
	// property constants
	public static final String CODE = "code";
	public static final String CODEFATHER = "codefather";
	public static final String CODELEVEL = "codelevel";
	public static final String CODENAME = "codename";
	public static final String FLAG = "flag";
	public static final String REMARK = "remark";
	public static final String RESERVE = "reserve";
	public static final String VALID = "valid";

	public void save(Chcode transientInstance) {
		log.debug("saving Chcode instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Chcode persistentInstance) {
		log.debug("deleting Chcode instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Chcode findById(java.lang.String id) {
		log.debug("getting Chcode instance with id: " + id);
		try {
			Chcode instance = (Chcode) getSession().get(
					"com.kylinhunter.hibernate.dao.Chcode", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Chcode instance) {
		log.debug("finding Chcode instance by example");
		try {
			List results = getSession().createCriteria(
					"com.kylinhunter.hibernate.dao.Chcode").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Chcode instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Chcode as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCode(Object code) {
		return findByProperty(CODE, code);
	}

	public List findByCodefather(Object codefather) {
		return findByProperty(CODEFATHER, codefather);
	}

	public List findByCodelevel(Object codelevel) {
		return findByProperty(CODELEVEL, codelevel);
	}

	public List findByCodename(Object codename) {
		return findByProperty(CODENAME, codename);
	}

	public List findByFlag(Object flag) {
		return findByProperty(FLAG, flag);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByReserve(Object reserve) {
		return findByProperty(RESERVE, reserve);
	}

	public List findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	public List findAll() {
		log.debug("finding all Chcode instances");
		try {
			String queryString = "from Chcode";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Chcode merge(Chcode detachedInstance) {
		log.debug("merging Chcode instance");
		try {
			Chcode result = (Chcode) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Chcode instance) {
		log.debug("attaching dirty Chcode instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Chcode instance) {
		log.debug("attaching clean Chcode instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}